package com.qianfeng.controller;

import com.qianfeng.dto.Page;
import com.qianfeng.entity.Drug;
import com.qianfeng.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

@Controller
public class DrugController {
    @Autowired
    private DrugService drugService;

    @RequestMapping("/getDrugList")
    public String getDrugList(){
        return "06_pharmaceutical_administration";
    }
    @RequestMapping("/jumpAddDrug")
    public String jumpAddDrug(){
        return "06_01_pharmaceutical_administration_add";
    }

    @RequestMapping("/addDrug")
    public String addDrug(Drug drug, MultipartFile drImg, HttpServletRequest request){
        System.out.println(drug);
        System.out.println(drImg.getOriginalFilename());
        //获取图片路径
        String path=request.getServletContext().getRealPath("/upload");
        System.out.println(path);
        File f = new File(path);
        if (!f.exists()){
            f.mkdirs();
        }
        //获取图片类型
        String filename=drImg.getOriginalFilename();
        filename = new Date().getTime()+filename;
        try {
            drImg.transferTo(new File(path+"/"+filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String imgUrl="http://localhost:8080/upload/"+filename;
        drug.setDrUrl(imgUrl);
        drug.setDrTime(new Date());
        drugService.addDrug(drug);
        return "06_pharmaceutical_administration";
    }
    @RequestMapping("/updateDrug")
    public String updateDrug(Drug drug, @RequestParam("drImg") MultipartFile drImg, HttpServletRequest request){
        System.out.println(drug);
        System.out.println(drImg.getOriginalFilename());
        //获取图片路径
        String path=request.getServletContext().getRealPath("/upload");
        System.out.println(path);
        File f = new File(path);
        if (!f.exists()){
            f.mkdirs();
        }
        //获取图片类型
        String filename=drImg.getOriginalFilename();
        filename = new Date().getTime()+filename;
        try {
            drImg.transferTo(new File(path+"/"+filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String imgUrl="http://localhost:8080/upload/"+filename;
        drug.setDrUrl(imgUrl);
        drug.setDrTime(new Date());
        drugService.updateDrug(drug);
      return "06_pharmaceutical_administration";
    }
    @ResponseBody
    @RequestMapping("/getDrugPage")
    public Page getDrugPage(Integer pageNo,Integer pageSize){
      int pageNo_=1;
      int pageSize_=2;
      if (pageNo!=null){
          pageNo_=pageNo;
      }
      if (pageSize!=null){
          pageSize_=pageSize;
      }
        Page page = drugService.getDrugPage(pageNo_, pageSize_);
        return page;
    }

    @RequestMapping("/getDrugDetail")
    public String getDrugDetail(Integer drId, Map map){
        Drug drug = drugService.getDrugDetailByDrId(drId);
        map.put("drug",drug);
        return "06_04_pharmaceutical_administration_detail";
    }
    @RequestMapping("/jumpAddDrugStore")
    public String jumpAddDrugStore(Integer drId,Map map){
        Drug drug = drugService.getDrugDetailByDrId(drId);
        map.put("drug",drug);
        return "06_03_pharmaceutical_administration_add_stock";
    }
    @RequestMapping("/addDrugStore")
    public String addDrugStore(Integer drId,Integer num){
        /*System.out.println(drId);
        System.out.println(num);*/
        drugService.addDrugStore(drId,num);
        return "06_pharmaceutical_administration";
    }
    @RequestMapping("/jumpUpdateDrug")
    public String jumpUpdateDrug(Integer drId,Map map){
        Drug drug = drugService.getDrugDetailByDrId(drId);
        map.put("drug",drug);
        return "06_02_pharmaceutical_administration_update";
    }



}
